package com.highspeed.bdk.service.physical;

import com.highspeed.bdk.bean.entity.physical.PhysicalLink;
import com.highspeed.bdk.repository.physical.PhysicalLinkRepository;
import com.highspeed.bdk.service.base.BdkBaseService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenjiabin on 2018/3/23.
 *
 * @see IPhysicalLinkService
 */
@Service
@Transactional
public class PhysicalLinkService extends BdkBaseService<PhysicalLink, Integer> implements IPhysicalLinkService {
    private static final Log LOG = LogFactory.getLog(PhysicalLinkService.class);
    @Autowired
    private PhysicalLinkRepository physicalLinkRepository;

    /**
     * @see IPhysicalLinkService
     */
    @Override
    public PhysicalLinkRepository getRepository() {
        return physicalLinkRepository;
    }

    /**
     * @see IPhysicalLinkService
     */
    @Override
    public void deleteLinksByMachineBoxId(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.board.machineBox.id_eq", id);
        params.put("rightCpu.board.machineBox.id_eq_or", id);
        List<PhysicalLink> links = queryToList(params, null);
        getRepository().deleteAll(links);
    }

    /**
     * @see IPhysicalLinkService
     */
    @Override
    public void deleteLinksByPhysicalTopologyId(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.board.machineBox.physicalTopology.id_eq", id);
        params.put("rightCpu.board.machineBox.physicalTopology.id_eq_or", id);
        List<PhysicalLink> links = queryToList(params, null);
        getRepository().deleteAll(links);
    }

    /**
     * @see IPhysicalLinkService
     */
    @Override
    public void deleteLinksByBoardId(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.board.id_eq", id);
        params.put("rightCpu.board.id_eq_or", id);
        List<PhysicalLink> links = queryToList(params, null);
        getRepository().deleteAll(links);
    }

    /**
     * @see IPhysicalLinkService
     */
    @Override
    public void deleteLinksByCpuId(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.id_eq", id);
        params.put("rightCpu.id_eq_or", id);
        List<PhysicalLink> links = queryToList(params, null);
        getRepository().deleteAll(links);
    }

    /**
     * @see IPhysicalLinkService
     */
    @Override
    public List<PhysicalLink> findByPhysicalTopologyId(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.board.machineBox.physicalTopology.id_eq", id);
        params.put("rightCpu.board.machineBox.physicalTopology.id_eq_or", id);
        List<PhysicalLink> links = queryToList(params, null);
        return links;
    }


    /**
     * @see IPhysicalLinkService
     */
    @Override
    public List<PhysicalLink> findByMachineBoxId(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.board.machineBox.id_eq", id);
        params.put("rightCpu.board.machineBox.id_eq_or", id);
        List<PhysicalLink> links = queryToList(params, null);
        return links;
    }


    /**
     * @see IPhysicalLinkService
     */
    @Override
    public List<PhysicalLink> findByBoardId(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.board.id_eq", id);
        params.put("rightCpu.board.id_eq_or", id);
        List<PhysicalLink> links = queryToList(params, null);
        return links;
    }

    /**
     * @see IPhysicalLinkService
     */
    @Override
    public List<PhysicalLink> findByCpuId(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.id_eq", id);
        params.put("rightCpu.id_eq_or", id);
        List<PhysicalLink> links = queryToList(params, null);
        return links;
    }

    @Override
    public PhysicalLink findByTwoCpuId(Integer leftCpuId, Integer rightCpuId) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.id_eq", leftCpuId);
        params.put("rightCpu.id_eq", rightCpuId);
        List<PhysicalLink> links = queryToList(params, null);
        if (links != null && !links.isEmpty()) {
            return links.get(0);
        }
        return null;
    }

    @Override
    public List<PhysicalLink> findAllByTwoCpuId(Integer leftCpuId, Integer rightCpuId) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        params.put("leftCpu.id_eq", leftCpuId);
        params.put("rightCpu.id_eq", rightCpuId);
        List<PhysicalLink> links = queryToList(params, null);
        return links;
    }
}
